import Vue from 'vue';
import Router from 'vue-router';
import {getLocalStorage} from '@/utils/local-storage';

import home from './home';
import items from './items';
import user from './user';
import order from './order';
import login from './login';
import bean from './bean';
import {Toast} from "vant";

Vue.use(Router);
Toast.setDefaultOptions({ duration: 1000 });

const RouterModel = new Router({
    routes: [...home, ...items, ...user, ...order, ...login, ...bean]
});

RouterModel.beforeEach((to, from, next) => {
    const {Authorization} = getLocalStorage(
        'Authorization'
    );
    if (!Authorization) {
        if (to.meta.login) {
            // Toast.fail('请登录');
            setTimeout(() => {
                next({name: 'login', query: {redirect: to.name}});
            }, 1000)
            return;
        }
    }
    next();
});

export default RouterModel;
